﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using COFE.Core.Profile;
using COFE.Core.Utils;
using COFE.Core.Entry;

namespace COFE.Core.Lister
{
    public class CorePropertyProvider : ISecondaryPropertyProvider<IFileSystemInfo>
    {
        public object[] GetSupportedProperty(IFileSystemInfo sourceObj)
        {
            var primPropProvider = sourceObj.InternalProperties.PrimaryPropertyProvider;

            List<object> retVal = new List<object>();

            if (sourceObj is IFileInfo || primPropProvider.GetProperty(DirectoryInfoProperties.IsFileBasedFS).ValueAsBoolean)
            {
                retVal.Add(FileInfoProperties.CRC);
                retVal.Add(FileInfoProperties.MD5);
            }
            retVal.Add(FileSystemInfoProperties.EntryTypeInfo);
            retVal.Add(FileSystemInfoProperties.EntryTypeInfoKey);

            return retVal.ToArray();
        }

        public bool IsPropertyReadOnly(IFileSystemInfo sourceObj, object property)
        {
            return true;
        }

        public PropertyPair GetProperty(IFileSystemInfo sourceObj, object property)
        {
            switch ((int)property)
            {
                case (int)FileInfoProperties.CRC:
                case (int)FileInfoProperties.MD5:

                    IFileInfo file = null;
                    if (sourceObj is IFileInfo)
                        file = sourceObj as IFileInfo;
                    else
                    {
                        var primPropProvider = sourceObj.InternalProperties.PrimaryPropertyProvider;
                        if (primPropProvider.GetProperty(DirectoryInfoProperties.IsFileBasedFS).ValueAsBoolean)
                            file = primPropProvider.GetProperty(DirectoryInfoProperties.FileBasedFSFile).Value as IFileInfo;
                    }

                    if (file != null)
                        using (var fs = file.OpenRead())
                        {
                            if (property.Equals(FileInfoProperties.CRC))
                                return new PropertyPair(property, fs.GetCRC());
                            else return new PropertyPair(property, fs.GetMD5());
                        }

                    return new PropertyPair(property, "");
                    
                case (int)FileSystemInfoProperties.EntryTypeInfoKey: 
                    return new PropertyPair(property, COFEShared.EntryTypeDictionary[sourceObj].ID);

                case (int)FileSystemInfoProperties.EntryTypeInfo:
                    return new PropertyPair(property, COFEShared.EntryTypeDictionary[sourceObj]);

                default:
                    throw new NotImplementedException();
            }


        }

        public void SetProperty(IFileSystemInfo sourceObj, object property, object value)
        {
            throw new NotImplementedException();
        }
    }
}
